<!--
	作者：offline
	时间：2018-09-03
	描述：实物对账单
-->
<template>
	<div class="m-view-goodsAccountBill m-window">
		<div class="m-condition" v-if="diaShow">
			<el-row :gutter="10">
				<el-form
					:model="ruleForm"
					ref="ruleForm"
					:label-width="labelWidth"
					:inline-message="true"
					label-position="left"
				>
					<el-col :span="7">
						<el-form-item :label="label" class="rightItem">
							<el-select
								value-key="id"
								v-model="selCompanyCode"
								:placeholder="placeholder"
								@change="companyChange"
								ref="content"
								:disabled="btnDis"
								filterable
                                :clearable="accountType==4"
							>
								<el-option
									v-for="item in comOptions"
									:key="item.id"
									:label="item.companyName"
									:value="item"
								></el-option>
							</el-select>
						</el-form-item>
					</el-col>
                    <el-col :span="7">
						<el-form-item label="申请日期" class="rightItem">
							<el-date-picker
								v-model="applySDate"
								type="date"
								placeholder="开始日期"
								value-format="yyyy-MM-dd"
								:clearable="false"
								:editable="false"
								:picker-options="po1"
								:disabled="btnDis"
							></el-date-picker>
							<!--@change="applySDateChange"-->
						</el-form-item>
					</el-col>
					<el-col :span="5">
						<el-date-picker
							v-model="applyEDate"
							type="date"
							placeholder="结束日期"
							value-format="yyyy-MM-dd"
							:clearable="false"
							:editable="false"
							:picker-options="po2"
							:disabled="btnDis"
						></el-date-picker>
						<!--@change="applyEDateChange"-->
					</el-col>
					<el-col :span="5">
						<el-form-item label="对账单号" class="rightItem">
							<el-input v-model="accountNo" placeholder="请输入单号" clearable></el-input>
						</el-form-item>
					</el-col>
				</el-form>
			</el-row>
			<el-row :gutter="10">
				<el-form
					:model="ruleForm"
					ref="ruleForm"
					label-width="70px"
					:inline-message="true"
					label-position="left"
				>
                <el-col :span="7" v-if="!btnShow">
						<el-form-item label="供应商" class="rightItem">
							<el-input
								placeholder="请选择供应商"
								v-model="selCusName"
								clearable
								@focus="showSupplierDialog"
								readonly
								@mouseenter.native="mouseEnter"
								@mouseleave.native="mounseLeave"
								:disabled="btnDis"
							>
								<el-button slot="append" icon="el-icon-search" @click="showSupplierDialog"></el-button>
								<span slot="suffix" @click="inputClearSon" class="m-cleanInput" v-show="inputClearBut">
									<i class="el-input_icon el-icon-circle-close el-input__clear"></i>
								</span>
							</el-input>
						</el-form-item>
					</el-col>
					

					<el-col :span="7">
						<el-form-item label="单据状态" class="rightItem">
							<el-select v-model="recCode" placeholder="单据状态" clearable :disabled="btnDis">
								<!--@change="recCode_change"-->
								<el-option
									v-for="item in recOptions"
									:key="item.value"
									:label="item.label"
									:value="item.value"
								></el-option>
							</el-select>
						</el-form-item>
					</el-col>
                    <el-col :span="2" v-if="btnShow" class="m-floatRight">
						<el-button type="primary" @click="del" :disabled="btnDis">删除</el-button>
					</el-col>
					<el-col :span="3" class="m-floatRight">
						<el-button type="primary" @click="checkSteps" :disabled="btnDis">查看流程信息</el-button>
					</el-col>
                    <el-col :span="2" class="m-floatRight">
						<el-button type="primary" @click="search(1)" :disabled="btnDis">查询</el-button>
					</el-col>
				</el-form>
			</el-row>
		</div>
		<div class="m-agTable m-bottom m-top2" v-if="diaShow">
			<physicalCheckInfo ref="getPhysicalInfo" @getClickValue="getClickValue"></physicalCheckInfo>
		</div>
		<div class="m-page" v-if="diaShow">
			<el-pagination
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				@prev-click="handlePrevClick"
				@next-click="handleNextClick"
				:current-page="currentPage"
				:page-sizes="[30,50,100,150,200]"
				:page-size="pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="total"
			></el-pagination>
		</div>

		<el-dialog
			title="供应商信息"
			v-if="srmSupplierDialogVisible"
			:visible.sync="srmSupplierDialogVisible"
			width="50%"
			:before-close="srmSupplierDialogClose"
			v-dialogDrag
			:close-on-click-modal="false"
			:close-on-press-escape="false"
		>
			<srmSupplierInfo ref="srmSupplierInfo" :layout="layout" @setSrmSupplierInfo="setSrmSupplierInfo"></srmSupplierInfo>
		</el-dialog>

		<!--新增实物对账-->
		<div class="m-window" v-if="!diaShow">
			<physicalApplyComponents
				@cancelComponents="cancelComponents"
				:pageLayOut="pageLayOut"
				:setClickValue="setClickValue"
			></physicalApplyComponents>
		</div>
		<!-- 查看流程信息弹窗 -->
		<accountSteps
			:dialogTitle="'实物对账流程信息'"
			:stepsVisible="stepsVisible"
			:stepsData="stepsData"
			:activeIndex="activeIndex"
			@dialogclose="closeSteps"
		></accountSteps>
	</div>
</template>

<script>
import _Api from "@/api";
// import localeText from "@/i18n/agGridText";
import serviceTime from "@/public/serverTime.js";
import srmSupplierInfo from "@/components/srmSupplierInfo.vue";
import physicalCheckInfo from "@/views/accountManagement/physicalCheckInfo.vue"
// import accountCheckInfo from "@/views/accountManagement/accountCheckInfo.vue"
// import orderInfo from "@/views/accountManagement/orderInfo.vue"
// import storageInfo from "@/views/accountManagement/storageInfo.vue"
import physicalApplyComponents from "@/views/accountManagement/physicalApplyComponents.vue"
import accountSteps from "@/components/accountSteps.vue";
import constant from "@/public/constant";
import Msg from "@/public/message.js";

export default {
	props: {
		isRefresh: {
			type: Boolean
		}
	},
	data () {
		return {
			labelWidth: '68px',
			label: "",
			ruleForm: {},
			selCusCode: "",
			selCusName: "",
			cusOptions: [],
			selSupplierCode: "",
			supOptions: [],
			selCompanyCode: "",
			comOptions: [],
			accountNo: "",
			applySDate: "",
			applyEDate: "",
			recCode: "",
			//单据状态
			recOptions: [],//				
			diaShow: true,   //显示列表查询界面, 否则显示实物对账单详情界面
			//phyShow:false,  
			btnShow: true,   //显示供应商用户相关的控件，否则显示非供应商相关控件
			//optionsShow:false, 
			currentPage: 1,
			pageSize: 30,
			total: 0,
			physicalInfo: [],
			pageLayOut: "add",//页面配置，判断当前属于编辑/新增
			setClickValue: {},
			placeholder: "",
			supplierInfo: "",
			supplierName: "",
			srmSupplierDialogVisible: false,
			//auth:true,
			layout: {},
			inputClearBut: false,
			btnDis: false,
			po1: {
				disabledDate: this.disabledDate1
			},
			po2: {
				disabledDate: this.disabledDate2
			},
			current: 0,
			textChange: 0,
			// 是否显示流程信息弹窗
			stepsVisible: false,
			// 当前流程数据
			stepsData: [],
			// 步骤进行到哪一步
            activeIndex: 0,
            // 账户类型
            accountType:this.$global.user.userTypeId
		}
	},
	watch: {
		applyEDate (value) {
			this.current = 1;
		},
		recCode (val) {
			this.current = 1;
		},
		selCusName (val) {
			this.currentPage = 1;
			this.search();
		},
		accountNo (val) {
			if (val)
				this.textChange = 1;
			else
				this.textChange = 0;
		},
		applySDate (value) {
			this.current = 1;
			if (value == null)
				this.applySDate = "";
		},
		isRefresh () {
			this.cancelComponents(true)
		}
	},
	components: {
		physicalApplyComponents,
		physicalCheckInfo,
		srmSupplierInfo,
		accountSteps
	},
	created () {
	},
	methods: {
		// 查看流程信息
		checkSteps () {
			var that = this;
			this.stepsData = [];
			var row = this.$refs.getPhysicalInfo.$refs['agGridComponent'].gridOptions.api.getSelectedRows();
			if (row.length > 1) {
				Msg.showPopMsg("提示：不能同时查询多张实物对账单的流程！")
				return;
			} else if (row.length == 0) {
				Msg.showPopMsg("提示：请选择需要查看流程的实物对账单！")
				return;
			}

			var obj = {
				id: row[0].id
			}

			_Api.getGoodsBillProcess(obj).then(function (response) {
				var data = response.data;
				if (data.code == 1) {
					if (data.rows.length > 0) {
						data.rows.forEach((item, index) => {
							if (item.processStatus != 0) {
								var step = {
									name: item.processName,
									prcsDate: item.handleDate,
									result: item.processStatus
								}

								if (item.processStatus === 2) {
									that.activeIndex = index + 1;
								}
								that.stepsData.push(step);
							} else {
								var step = {
									name: item.processName,
									result: item.processStatus
								}
								that.stepsData.push(step);
							}
						})
					} else {
						Msg.showErrorMsg(data.info);
						return;
					}
				}
			})

			this.stepsVisible = true
		},
		// 关闭弹窗
		closeSteps () {
			this.stepsVisible = false
		},
		disabledDate1 (time) {
			if (this.applyEDate)
				return time.getTime() > new Date(this.applyEDate).getTime();
		},
		disabledDate2 (time) {
			if (this.applySDate)
				return time.getTime() < (new Date(this.applySDate).getTime() - 3600 * 1000 * 24);
		},
		applySDateChange (params) {
			this.currentPage = 1;
			this.search();
		},
		applyEDateChange (params) {
			this.currentPage = 1;
			this.search();
		},
		mounseLeave () {
			if (this.selCusName != "")
				this.inputClearBut = false;
		},
		mouseEnter () {
			if (this.selCusName != "")
				this.inputClearBut = true;
		},
		inputClearSon () {
			this.selCusCode = "";
			this.selCusName = "";
			this.inputClearBut = false;
		},
		setSrmSupplierInfo (msg) {
			if (msg.length > 0) {
				this.srmSupplierDialogVisible = false;
				this.selCusCode = msg[0].id;
				this.selCusName = msg[0].supplierName;
				// this.inputClearBut = true;
			}
		},
		srmSupplierDialogClose (done) {
			this.$refs.srmSupplierInfo.keyWord = "";
			this.$refs.srmSupplierInfo.selSupplierOptions.api.setQuickFilter(this.$refs.srmSupplierInfo.keyWord);
			done();
		},
		showSupplierDialog () {
			var that = this;
			if (!this.layout.companyCode) {
				Msg.showPopMsg("提示：请选择公司！")
				this.$refs.content.focus();
				return;
			} else {
				this.srmSupplierDialogVisible = true;
				this.$nextTick(_ => {
					that.$refs.srmSupplierInfo.search();
				});
			}
		},
		//单据状态选择
		recCode_change () {
			this.currentPage = 1;
			this.search();
		},
		//公司下拉框选择
		companyChange (value) {
			if (value) {
				// var obj = {
				// 	name:value.companyCode
				// };
				// var that = this;
				this.layout.companyCode = value.companyCode;
				this.layout.auth = true;
				//this.layout.filterByUser = true; //jjc 取消20181205
				if (this.selCusCode)
					this.selCusCode = "";
				if (this.selCusName)
					this.selCusName = "";
			} else {
				if (this.selCusCode)
					this.selCusCode = "";
				if (this.selCusName)
					this.selCusName = "";
				this.layout = {};
				//					this.getAuthInfo();
			}
			this.currentPage = 1;
			this.search();
		},
		search (flag) {
			//查询方法
			var obj = {};
			if (this.$global.user.userTypeId == "4") {
				obj.supplierId = constant.supplierId;
				obj.userType = 2;//供应商用户
				if (obj.supplierId == '') {
					this.$alert("提示：供应商编码不能为空", {
						showClose: false,
						confirmButtonText: "确定",
						customClass: "alertPrompt"
					});
					return;
				}
			}
			else {
                if (this.selCompanyCode == "") {
				Msg.showPopMsg("提示：" + this.placeholder)
				return;
			}
				obj.userType = 1;//内部用户
				if (this.selCusCode != "")
					obj.supplierId = this.selCusCode;
			}
			if (this.selCompanyCode)
				obj.companyId = this.selCompanyCode.id;
			this.accountNo = this.accountNo.replace(/\s+/g, "");
			if (this.accountNo)
				obj.gooAccMxMsgId = this.accountNo;

			//jjc20181014
			if (this.applySDate != "" && this.applySDate != null) {
				obj.begin_date = this.applySDate;
				if (this.applyEDate == "" || this.applyEDate == null)
					obj.end_date = obj.begin_date;
			}
			if (this.applyEDate != "" && this.applyEDate != null) {
				obj.end_date = this.applyEDate;
				if (this.applySDate == "" || this.applySDate == null)
					obj.begin_date = obj.end_date;
			}
			if (obj.begin_date != "" && obj.end_date != "") {
				if (obj.begin_date > obj.end_date) {
					Msg.showPopMsg("提示：开始日期不能大于结束日期")
					return;
				}
			}
			if (this.recCode == "" || !this.recCode)
				obj.billState = 0;
			else
				obj.billState = this.recCode;

			if (flag === 1)
				this.currentPage = 1;
			obj.current = this.currentPage;
			obj.size = this.pageSize;

			var that = this;

			this.$refs.getPhysicalInfo.loading = true;
            this.btnDis = true;
            that.$refs.getPhysicalInfo.setColumnDefs()
			_Api.getMyGoodList(obj).then(function (response) {
				var data = response.data;
				that.current = 0;
				that.textChange = 0;
				that.btnDis = false;
				if (data.code == "1") {
					if (data.total >= 0) {
						that.physicalInfo = data.rows;
						that.total = data.total;
						that.$refs.getPhysicalInfo.getDataRows(that.physicalInfo);
					}
					else {
						that.$refs.getPhysicalInfo.getDataRows([]);
					}
					that.$refs.getPhysicalInfo.loading = false;
				} else {
					Msg.showErrorMsg(data.info);
					that.btnDis = false;
					that.$refs.getPhysicalInfo.loading = false;
				}
			}).catch((err) => {
				Msg.showErrorMsg(err.data.message);
				that.$refs.getPhysicalInfo.loading = false;
				that.btnDis = false;
				that.current = 0;
				that.textChange = 0;
			})
		},

		handleSizeChange (params) {
			this.pageSize = params;
			this.currentPage = 1;
			this.search();
		},
		handleCurrentChange (params) {
			if (this.textChange === 1 || this.current === 1)
				this.currentPage = 1;
			else
				this.currentPage = params;
			this.search();
		},
		handlePrevClick (params) {
			if (this.textChange === 1 || this.current === 1)
				this.currentPage = 1;
			else
				this.currentPage = params;
			// this.handleCurrentChange(this.currentPage);
		},
		handleNextClick (params) {
			if (this.textChange === 1 || this.current === 1)
				this.currentPage = 1;
			else
				this.currentPage = params;
			// this.handleCurrentChange(this.currentPage);
		},
		addPhyCheck () {
			//新增实物对账
			this.diaShow = false;
			//this.phyShow = true;
			this.supplierName = this.$global.user.employeeName + ' (' + this.$global.user.userName + ')';
		},
		del () {
			this.$refs.getPhysicalInfo.getSelectedRow();
		},
		cancelComponents (msg) {
			if (msg == true) {
				var that = this;
				//this.phyShow = false;
				this.diaShow = true;
				console.log(this.diaShow)
				this.pageLayOut = "add";
				if (this.selCompanyCode.id) {
					this.$nextTick(_ => {
						that.search(this.selCompanyCode.id);
					})
				}
			}
		},
		getClickValue (msg) {
			if (msg != "" && msg) {
				this.diaShow = false;
				//this.phyShow = true;
				this.pageLayOut = "edit";
				this.setClickValue = msg;
			}
		},

		initControl () {
			//
			if (constant.user.userTypeId == 4)//供应商
			{
				this.placeholder = "请选择客户"
				this.label = "客户";
				this.btnShow = true;
				//单据状态
				this.recOptions = [
					{ value: 1, label: "保存" },
					{ value: 2, label: "提交" },
					{ value: 3, label: "已确认" }];
				//默认单据状态：提交
				this.recCode = "";
			}
			else {
				this.placeholder = "请选择公司";
				this.label = "公司";
				this.labelWidth = '70px';
				this.btnShow = false;//新增实物对账和删除按钮				
				//单据状态
				this.recOptions = [
					{ value: 2, label: "未确认" },
					{ value: 3, label: "已确认" }];
				//默认单据状态：未确认
				this.recCode = "";
			}
			var that = this;
			//默认日期范围 三个月
			serviceTime.getServiceTime().then((serviceTime) => {
				if (serviceTime) {
					var resultDate;
					var year = serviceTime.getFullYear();
					var month = serviceTime.getMonth() + 1;
					var month2;
					var dataArr = [];
					dataArr.push(serviceTime.Format("yyyy-MM"));
					var day = 0;
					for (var i = 0; i < 2; i++) {
						switch (month) {
							case 1:
								year--;
								month += 11;
								break;
							default:
								month--;
								break;
						}
						month2 = month;
						month2 = (month2 < 10) ? ('0' + month2) : month2;
						resultDate = year + '-' + month2;
						dataArr.push(resultDate);
					}
					for (let i in dataArr) {
						var data = dataArr[i].split('-');
						day += new Date(data[0], data[1], 0).getDate();
					}
					that.applySDate = new Date(serviceTime.getTime() - 3600 * 1000 * 24 * day).Format('yyyy-MM-dd');
				}
				that.applyEDate = serviceTime.Format("yyyy-MM-dd");
			})
		},

		initData () {
			//查询公司列表
			var obj = {};
			if (constant.user.userTypeId == 4) {
				obj.account = constant.user.userName;
				obj.userType = 2;
			}
			else {
				obj.account = constant.user.oaNum;
				obj.userType = 1;
			}
			var that = this;
			_Api.getAuthInfo(obj).then(function (response) {
				var data = response.data;
				if (data.code == "1") {
					that.btnDis = false;
					that.comOptions = data.rows.myMstCompany;
					//默认公司
					if (constant.user.userTypeId != 4) {
						//找登录人所在公司的条目
						for (var i = 0; i < that.comOptions.length; i++) {
							if (that.comOptions[i].companyCode == constant.user.bukrs) {
								that.selCompanyCode = that.comOptions[i];
							}
						}
					}
					else {
						//找第一家公司
						that.selCompanyCode = that.comOptions[0];
					}
					that.layout.companyCode = that.selCompanyCode.companyCode;
					that.layout.auth = true;
					//自动查询
					that.search(1);
				}
				else {
					that.comOptions = [];
					that.cusOptions = [];
					that.layout = {};
				}

			}).catch((err) => {
				Msg.showErrorMsg(err.data.message);
				that.comOptions = [];
				that.cusOptions = [];
			})
		}
	},
	mounted () {
		//初始化控件
		this.initControl()
		//初始化数据源
		this.initData();
	}



}
</script>

<style>
</style>
